package managers;

import java.sql.Time;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import entities.Category;
import entities.Event;
import entities.User;


@Stateless
public class manEvent {
	@PersistenceContext
    private EntityManager em;
	

	public boolean addEvent(String name,String des,String place,String hour,Date date,int nb,int idCate,String idUser){
		boolean flag=false;
		Event  mEv=new Event();
		mEv.setName(name);
		mEv.setDescription(des);
		mEv.setPlace(place);
		mEv.setHour(hour);
		mEv.setDate(date);
		mEv.setNbParticipants(nb);
		//reste a finir 
		Category nCat=em.find(Category.class,idCate);
		nCat.addEvent(mEv);
		User promoter=em.find(User.class, idUser);
		System.out.println(idUser);
		promoter.proEvent(mEv);
		
		em.persist(promoter);
		em.persist(nCat);
		em.persist(mEv);
		flag=true;
		return flag;	
	}
	public Event getEvent(int id){
		Event nEvent=em.find(Event.class,id);
		return nEvent;
	}
	public List<Event> allEvent(){
		TypedQuery<Event> request = em.createQuery("select c from Event c where c.open='1'", Event.class);
		if (request==null)
			System.out.println("request null");
		return request.getResultList();
	}  
	public boolean delEvent(int id){
		boolean flag=false;
		Event nEvent=em.find(Event.class,id);  
	    em.remove(nEvent); 
		flag=true;
		return flag;
	}
	public boolean updateEvent(int id,String name,String des,String place,String hour,Date date,int nb,int idCate){
		boolean flag=false;
		Event nEvent=em.find(Event.class,id);   
        em.clear(); //把实体管理器中的所有实体变成游离状态。  
        nEvent.setName(name);
        nEvent.setDescription(des);
        nEvent.setHour(hour);
        nEvent.setDate(date);
        nEvent.setNbParticipants(nb);
        
		Category nCat=em.find(Category.class,nEvent.getCategory().getIdCategory());
		nCat.delEvent(nEvent);
		
		Category mCat=em.find(Category.class,idCate);
		mCat.addEvent(nEvent);
		
		em.merge(nCat);
		em.merge(mCat);
        em.merge(nEvent);  
        flag=true;
		return flag;
	}
	

	public boolean addParticipant(int idEvent,String idUser){
		boolean flag=false;
		
		Event nEvent=em.find(Event.class,idEvent);   
        em.clear(); //把实体管理器中的所有实体变成游离状态。
        
		User participer=em.find(User.class, idUser);
		List<Event> evn= participer.getParEvents();
		
		if(!evn.contains(nEvent)) {
			System.out.println(idUser);
			participer.parEvent(nEvent);
			
			nEvent.addParticipants(participer);
			
			em.merge(participer);
			em.merge(nEvent);
			flag=true;
		}
		
		else {
			System.out.println("Deja participe");
			flag=false;
		}
		
		
		return flag;
	}
	
	public boolean delParticipant(int idEvent,String idUser){
		boolean flag=false;
		
		
		Event nEvent=em.find(Event.class,idEvent);   
        em.clear(); //把实体管理器中的所有实体变成游离状态。
        
        System.out.println(nEvent.getIdEvent());
		User participer=em.find(User.class, idUser);
		System.out.println(idUser+"Me llamaron");
		//
		List<User> debug = nEvent.getParticipants();
		System.out.println(debug.size()+"Taille de participants");
		//String del = participer.getUser();
		//Iterator trol = debug.iterator();
		
		//if(debug.contains(participer)) 
		//	System.out.println("Destoru duud");
		//while (trol.hasNext()) {
		//	System.out.println(trol.next());//.toString();
		//	System.out.println(participer);
			//System.out.println(trol.next());
			//if(participer.toString().equals(trol.next().toString()))
			//	System.out.println("It does detroy!");
			//else
			//	System.out.println("It does not compile");
			
		//}
		//System.out.println(" FINISH");
		//mng.removeParticipant(participer.getUser(), nEvent.getIdEvent());
		boolean work= nEvent.removeParticipants(participer);
		if(work) {
			System.out.println("Detroyed!");
		}
		debug = nEvent.getParticipants();
		System.out.println(debug.size()+"Taille de participants apres delete");
		////
		List<Event> debug2 = participer.getParEvents();
		System.out.println(debug2.size()+"Taille de participants apres delete");
		
		participer.delParEvent(nEvent);
		 debug2 = participer.getParEvents();
		System.out.println(debug2.size()+"Taille de participants apres delete");
		
		em.merge(nEvent);
		em.merge(participer);
		em.flush();
		flag=true;
		System.out.println(" FINISH");
		return flag;
	}
	
	public User searchById(String id) {
		TypedQuery<User> request = em.createQuery("select c from User c where user='"+id+"'",User.class);
		return request.getSingleResult();
	}
}